package com.czj.controller;

import cn.hutool.core.collection.CollUtil;
import com.czj.common.api.CommonResult;
import com.czj.dto.CardParam;
import com.czj.dto.UserLoginParam;
import com.czj.model.Card;
import com.czj.model.Permission;
import com.czj.model.User;
import com.czj.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户管理
 */
@Controller
@Api(tags = "UserController", description = "用户管理")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<User> register(@RequestBody User userParam, BindingResult result) {
        User user = userService.register(userParam);
        if (user == null) {
            CommonResult.failed();
        }
        return CommonResult.success(user);
    }



    @ApiOperation(value = "登录以后返回token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestBody UserLoginParam userLoginParam, BindingResult result) {
        String token = userService.login(userLoginParam.getUsername(), userLoginParam.getPassword());
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }

    @ApiOperation("获取用户所有权限（包括+-权限）")
    @RequestMapping(value = "/permission/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Permission>> getPermissionList(@PathVariable Integer userId) {
        List<Permission> permissionList = userService.getPermissionList(userId);
        return CommonResult.success(permissionList);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getAdminInfo(Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        User user = userService.getUserByUsername(username);
        return CommonResult.success(user);
    }

    @ApiOperation(value = "修改用户信息")
    @RequestMapping(value = "/updateUserById", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> updateUserById(@RequestBody User user){
        return userService.updateUserById(user);
    }

    @ApiOperation(value = "添加认证身份证")
    @RequestMapping(value = "/insertCard", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> insertCard(Principal principal,@RequestBody CardParam cardParam){
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        return userService.insertCard(username,cardParam);
    }

    @ApiOperation(value = "注销用户")
    @RequestMapping(value = "/deleteUserById", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> deleteUserById(Principal principal){
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        return userService.deleteUserById(username);
    }

    @ApiOperation(value = "通过手机号获取验证码")
    @RequestMapping(value = "/getCode", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getCode(Principal principal){
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        return userService.getCode(username);
    }

    @ApiOperation(value = "验证验证码是否正确")
    @RequestMapping(value = "/checkCode", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult checkCode(Principal principal,String code){
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        return userService.checkCode(username,code);
    }
}
